<template>
  <el-dialog
    title="修改密码"
    :visible="$store.state.update_pwd_dialog"
    width="40%"
    @close="closeUpdatePwdDialog"
  >
    <el-form
      :model="wx_gate_user_form"
      :rules="rules"
      label-width="100px"
      ref="wx_gate_user_form"
    >
      <el-form-item label="原密码" prop="oldPwd">
        <el-input
          type="password"
          v-model="wx_gate_user_form.oldPwd"
          placeholder="请输入原密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input
          type="password"
          v-model="wx_gate_user_form.newPwd"
          placeholder="请输入新密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="重复新密码" prop="againPwd">
        <el-input
          type="password"
          v-model="wx_gate_user_form.againPwd"
          placeholder="请再次输入新密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePwd">保存</el-button>
        <el-button @click="closeUpdatePwdDialog">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import WxGateUser from "@/api/WxGateUser";
import { mapMutations } from "vuex";
import aes from "@/tool/AES";
import strKit from "@/tool/StrKit";

export default {
  components: {},
  props: {},
  data() {
    return {
      api: null,
      wx_gate_user_form: {
        oldPwd: "",
        newPwd: "",
        againPwd: ""
      },
      rules: {
        oldPwd: [{ required: true, message: "请输入原密码", trigger: "blur" }],
        newPwd: [{ required: true, message: "请输入新密码", trigger: "blur" }],
        againPwd: [
          { required: true, message: "请再次输入新密码", trigger: "blur" }
        ]
      }
    };
  },
  computed: {},
  watch: {},
  created() {
    this.api = new WxGateUser();
  },
  methods: {
    ...mapMutations(["changeUpdatePwdDialog"]),
    updatePwd() {
      this.$refs["wx_gate_user_form"].validate(valid => {
        if (valid) {
          if (
            this.wx_gate_user_form.newPwd === this.wx_gate_user_form.againPwd
          ) {
            let salt = strKit.generateMixed(8);
            let paras = {
              oldPwd: aes.encrypt(this.wx_gate_user_form.oldPwd + salt),
              newPwd: aes.encrypt(this.wx_gate_user_form.newPwd + salt),
              salt: salt
            };
            this.api.changePassword(paras, res => {
              if (res.code === 0) {
                this.$message.success("修改成功");
                this.closeUpdatePwdDialog();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            this.$message.error("两次输入的密码不一致");
          }
        } else {
          return false;
        }
      });
    },
    closeUpdatePwdDialog() {
      this.wx_gate_user_form = {
        oldPwd: "",
        newPwd: "",
        againPwd: ""
      };
      this.$refs["wx_gate_user_form"].resetFields();
      this.changeUpdatePwdDialog(false);
    }
  }
};
</script>

<style lang="scss" scoped></style>
